﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleData.ObjectModel
{
	/// <summary>
	/// Representa o nome de um campos
	/// </summary>
	/// <remarks>
	/// Padrão da nomenclatura
	/// {namespace}[{fieldName}]
	/// </remarks>
	public class FieldName : DataObjectNameBase
	{
		public const string DefaultFieldNamespace = "Global.Fields";

		#region [ .ctor ]

		/// <summary>
		/// Utilizado pelo EF
		/// </summary>
		protected FieldName()
			: base()
		{ }

		public FieldName( string name )
			: this( name, DefaultFieldNamespace )
		{ }

		public FieldName( string name, string ns )
			: base( name, ns )
		{ }

		#endregion

		//#region [ Properties ]

		//public string Name
		//{
		//    get;
		//    private set;
		//}

		//public string Namespace
		//{
		//    get;
		//    private set;
		//}

		//public string FullName
		//{
		//    get 
		//    {
		//        if ( this.fullName == null )
		//            this.fullName = String.Concat( this.Namespace, "[", this.Name, "]" );
		//        return this.fullName; 
		//    }
		//}

		//#endregion

		//public override string ToString()
		//{
		//    return this.FullName;
		//}

		//public override bool Equals( object obj )
		//{
		//    return this.FullName.Equals( obj );
		//}

		//public override int GetHashCode()
		//{
		//    return this.FullName.GetHashCode();
		//}


		/// <summary>
		/// Tenta fazer a conversão da string <paramref name="name"/> para o objeto <see cref="FieldName"/>
		/// </summary>
		/// <param name="fullName"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public static bool TryParse( string fullName, out FieldName value )
		{
			value =
				DataObjectNameParser.TryParse(
					fullName,
					( ns, name ) => new FieldName( name, ns )
					{
						Namespace = ns,
						Name = name
					} );
			return value != null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fullName"></param>
		/// <returns></returns>
		public static FieldName Parse( string fullName )
		{
			FieldName name;

			if ( FieldName.TryParse( fullName, out name ) )
				return name;
			else
				throw new ArgumentException();
		}

		/// <summary>
		/// Verifica se o conteúdo de <paramref name="name"/> é um nome de
		/// campo válido
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static bool IsFullName( string name )
		{
			return DataObjectNameParser.IsFullName( name );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="name"></param>
		/// <returns></returns>
		public static bool OnlyName( string name )
		{
			return DataObjectNameParser.IsOnlyName( name );
		}
	}
}
